//
//  HMHomeTableViewController.swift
//  微博
//
//  Created by 程双岐 on 16/9/19.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import YYModel



private let HMHomeTableCellID = "HMHomeTableCellID"
class HMHomeTableViewController: HMVisitorViewController {

    fileprivate var statusListViewModel:HMStatusListViewModel = HMStatusListViewModel()
    
    fileprivate lazy var pullUpView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        view.color = UIColor.red
        return view
        
    }()
    
    fileprivate lazy var pullDownView: UIRefreshControl = {
        let view = UIRefreshControl()
        view.addTarget(self, action: #selector(pullDownViewRefresh), for: .valueChanged)
        
        return view
    
    }()
    
    fileprivate lazy var pullDownCtr: HMRefreshControl = {
        
        let view = HMRefreshControl()
        view.addTarget(self, action: #selector(pullDownViewRefresh), for: .valueChanged)
        return view
    }()
    
    fileprivate lazy var tipLable: UILabel = {
        let label = UILabel(textColor: UIColor.white, fontSize: 13)
        label.textAlignment = .center
        label.backgroundColor = UIColor.orange
        label.text = "没有加载到最新的新浪微博数据"
        label.isHidden = true
        return label
    }()
    
    private func setupUi() {
        if let nav = self.navigationController {
            nav.view.insertSubview(tipLable, belowSubview: nav.navigationBar)
            tipLable.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35 , width: nav.navigationBar.width, height: 35)
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isVisitor {
            visitor?.updateAisitorView(message: nil, imageName: nil)
        }else{
            loadData()
            setupTable()
            setupUi()
        }

    }
    //设置uitable的相关操作
    private func setupTable(){
        //UITableViewCell.self获取class
        tableView.register(HMHomeTableViewCell.self, forCellReuseIdentifier: HMHomeTableCellID)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 300
        tableView.separatorStyle = .none
        tableView.tableFooterView = pullUpView
        
//        tableView.addSubview(pullDownView)
//        self.refreshControl = pullDownView
        tableView.addSubview(pullDownCtr)
        
    }
    private func stopPullView(){
        pullUpView.stopAnimating()
        pullDownView.endRefreshing()
        pullDownCtr.endRefreshing()

    }
    
    @objc private func pullDownViewRefresh(){
        loadData()
    }
    
    private func startAnimation(message: String){
        if tipLable.isHidden == false {
            return
        }
        
        tipLable.text = message
        UIView.animate(withDuration: 1, animations: {
            self.tipLable.isHidden = false
            self.tipLable.transform = CGAffineTransform(translationX: 0, y: self.tipLable.height)
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: {
                self.tipLable.transform = CGAffineTransform.identity
                
                }, completion: { (_) in
                    self.tipLable.isHidden = true
                    
            })
        }
    }
    
    //加载首页微博数据
    func loadData(){
        
        //如果开启动画就是上拉加载，如果没开启动画就是下拉刷新
        statusListViewModel.loadData(isPullup: pullUpView.isAnimating) { (isSuccees,message) in
            
            
            if !self.pullUpView.isAnimating{
                self.startAnimation(message: message)
                
            }
            self.stopPullView()
            
            if isSuccees{
                
                self.tableView.reloadData()
            }
        }
        
        
    }
    
    
    
}

extension HMHomeTableViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: HMHomeTableCellID, for: indexPath) as? HMHomeTableViewCell
        cell?.statusViewModel = statusListViewModel.statusList[indexPath.row]
        cell?.selectionStyle = .none
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == statusListViewModel.statusList.count - 1 && !pullUpView.isAnimating{
            pullUpView.startAnimating()
            print("已经滚动到最后一个cell了")
            loadData()
        }
    }
}
